// pages/videoInfo/videoInfo.js
var videoUtils = require('../utils/videoUtils')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cover:"cover",
    videoId:"",
    src:"",
    videoInfo:{},
    userLikeVideo:false,
  },
  videoCtx:{},

  onLoad:function(params){
    var me = this;
    me.videoCtx = wx.createVideoContext('myVideo', me);
    
    //获取上一个页面传入的参数
    var videoInfo = JSON.parse(params.videoInfo);

    var height = videoInfo.videoHeight;
    var width = videoInfo.videoWidth;
    var cover = "cover";
    if (width >= height){
      cover = "";
    }
    me.setData({
      videoId:videoInfo.id,
      src: app.serverUrl + videoInfo.videoPath,
      videoInfo:videoInfo,
      cover:cover
    })

    var serverUrl = app.serverUrl;
    var user = app.getGloalUserInfo();
    var loginUserId = "";
    if (user != null && user != undefined &&  user != ''){
      loginUserId = user.id;
    }
    wx.request({
      url: serverUrl + "/user/queryPublisher?loginUserId=" + loginUserId + "&videoId=" + videoInfo.id + "&publishUserId=" + videoInfo.userId,
      method:"POST",
      success:function(res){
        var publisher = res.data.data.publisher;
        var userLikeVideo = res.data.data.userLikeVideo;
        
        me.setData({
          serverUrl:serverUrl,
          publisher:publisher,
          userLikeVideo: userLikeVideo,
        })

      }
    })
  },

  onShow:function(){
    var me = this;
    me.videoCtx.play();
  },

  onHide:function(){
    var me = this;
    me.videoCtx.pause();
  },

  showSearch:function(){
    wx.navigateTo({
      url: '../searchVideo/searchVideo',
    })
  },

  upload:function(){
    //对没有登录或者本地没有用户信息缓存时进行跳转要进行拦截
    var me = this;
    var user = app.getGloalUserInfo();
    var videoInfo = JSON.stringify(me.data.videoInfo);
    //需要使用#，@符号替代，因为url中存在？，以父的？为准
    var realUrl = "../videoInfo/videoInfo#videoInfo@" + videoInfo;

    if (user == null || user == undefined || user == ''){
      wx.navigateTo({
        url: '../userLogin/login?redirectUrl=' + realUrl,
      })
    }else{
      videoUtils.uploadVideo();
    }
  },

  showIndex:function(){
    wx.navigateTo({
      url: '../index/index',
    })
  },

  showMine:function(){
    //对没有登录或者本地没有用户信息缓存时进行跳转要进行拦截
    var me = this;
    var user = app.getGloalUserInfo();
    var videoInfo = JSON.stringify(me.data.videoInfo);
    //需要使用#，@符号替代，因为url中存在？，以父的？为准
    var realUrl = "../videoInfo/videoInfo#videoInfo@" + videoInfo;
    // wx.navigateTo({
    //   url: '../mine/mine',
    // })
    if (user == null || user == undefined || user == ''){
      wx.navigateTo({
        url: '../userLogin/login?redirectUrl=' + realUrl,
      })
    }else{
      wx.navigateTo({
        url: '../mine/mine',
      })
    }
  },

  likeVideoOrNot:function(){
    var me = this;
    var user = app.getGloalUserInfo();
    var videoInfo = me.data.videoInfo;
    var videoInfoStr = JSON.stringify(videoInfo);
    //需要使用#，@符号替代，因为url中存在？，以父的？为准
    var realUrl = "../videoInfo/videoInfo#videoInfo@" + videoInfoStr;
    if (user == null || user == undefined || user == ''){//前端拦截
      wx.navigateTo({
        url: '../userLogin/login?redirectUrl=' + realUrl,
      })
    }else{
      var userLikeVideo = me.data.userLikeVideo;
      var url = '/video/userLike?userId=' + user.id + "&videoId=" + videoInfo.id + "&videoCreateId=" + videoInfo.userId;
      if (userLikeVideo){
        var url = '/video/userUnlike?userId=' + user.id + "&videoId=" + videoInfo.id + "&videoCreateId=" + videoInfo.userId;
      }
      var serverUrl = app.serverUrl;
      wx.showLoading({
        title: '...',
      })
      wx.request({
        url: serverUrl + url,
        method:"POST",
        header:{
          'content-type':'application/json',
          'userId':user.id,
          'userToken': user.userToken
        },
        success:function(res){
          console.log(res)
          wx.hideLoading();
          if (res.data.status == 200){
            me.setData({
              userLikeVideo: !userLikeVideo
            })
          }else if(res.data.status == 502){//后端拦截
            wx.showToast({
              title: res.data.msg,
              duration: 3000,
              icon: "none",
              success:function(){
                wx.redirectTo({
                  url: '../userLogin/login?redirectUrl=' + realUrl,
                })
              }
            })
          }
        }
      })
    }
  },
  showPublisher:function(){
    var me = this;
    var user = app.getGloalUserInfo();

    var videoInfo = me.data.videoInfo;
    var realUrl = '../mine/mine#publisherId@' + videoInfo.publisherId;

    if (user == null || user == undefined || user == ''){
      wx.navigateTo({
        url: '../userLogin/login?redirectUrl=' + realUrl,
      })
    }else{
      wx.navigateTo({
        url: '../mine/mine?publisherId=' + videoInfo.userId,
      })
    }
  },
  shareMe:function(){
    var me = this;
    var user = app.getGloalUserInfo();
    wx.showActionSheet({
      itemList: ['下载到本地','举报用户','分享到朋友圈','分享到QQ空间','分享到微博'],
      success:function(res){
        if (res.tapIndex == 0){
          //下载到本地
          wx.showLoading({
            title: '下载中',
          })
          wx.downloadFile({
            url: app.serverUrl + me.data.videoInfo.videoPath,
            success:function(res){
              if(res.statusCode == 200){
                  console.log(res.tempFilePath)
                  //保存
                  wx.saveVideoToPhotosAlbum({
                    filePath: res.tempFilePath,
                    success:function(res){
                      console.log(res.errMsg)
                      wx.hideLoading()
                    }
                  })
              }
            }
          })
        }else if(res.tapIndex == 1){
          //举报用户
          var videoInfo = JSON.stringify(me.data.videoInfo);
          var realUrl = '../videoInfo/videoInfo#videoInfo@' + videoInfo;

          if (user == null || user == undefined || user == ''){
            wx.navigateTo({
              url: '../userLogin/login?redirectUrl=' + realUrl,
            })
          }else{
            var publishUserId = me.data.videoInfo.userId;
            var videoId = me.data.videoInfo.id;
            var currentUserId = user.id;
            wx.navigateTo({
              url: '../report/report?videoId=' + videoId + "&publishUserId=" + publishUserId,
            })
          }
        }else{
          wx.showToast({
            title: '官方尚未开放该功能....',
          })
        }
      }
    })
  },
  onShareAppMessage:function(res){
    var me = this;
    var videoInfo = me.data.videoInfo;

    return {
      title: '短视频内容分析',
      path: 'pages/videoInfo/videoInfo?videoInfo=' + JSON.stringify(videoInfo)
    }
  }

})